import round, cards

class Game:
    def __init__(self, rung):
        self.rung = rung
        self.round = round.Round(self.rung)
        self.moves = 0
        self.team1Count = 0
        self.team2Count = 0
        self.complete = False
        self.lastWin = None       
        self.firstWin = True 
        
    def addMove(self, player, move):
        if(self.moves == 13):
            print("this can't be happening")
            return -2
        else:
            self.round.addMove(player, move)
            if(self.round.roundComplete()):
                winner, winningCard = self.round.checkHand()
                self.round.refresh()
                self.moves += 1
                if(self.checkForWin(winner, winningCard)):
                    self.lastWin = None
                else:
                    self.lastWin = winner
                return winner
            else:
                return -1
         
    def checkForWin(self, cWinner, winningCard):
        if(self.moves < 4):
            return False
        elif(self.moves == 12):
            return False
        elif(self.moves == 13):
            if(cWinner == 0 or cWinner == 2):
                self.team1Count += self.moves - (self.team1Count + self.team2Count)
            else:
                self.team2Count += self.moves - (self.team1Count + self.team2Count)
            return True
        elif(cWinner == self.lastWin):
            if(self.firstWin):
                if(winningCard.num == 14):
                    return False
                else:
                    self.firstWin = False
            if(cWinner == 0 or cWinner == 2):
                self.team1Count += self.moves - (self.team1Count + self.team2Count)
            else:
                self.team2Count += self.moves - (self.team1Count + self.team2Count)
            return True
        else:
            return False
        
    
    def getRound(self):
        return self.round.getRound()
                    
    def gameComplete(self):
        if(self.moves == 13):
            return True
        else:
            return False
        
    def getWins(self):
        return (self.team1Count, self.team2Count)
    
    def getWinner(self):
        if(self.team1Count > self.team2Count):
            return 1;
        else:
            return 2;
         
        
        
        
        